package com.wc.contrller;

import ch.qos.logback.classic.Logger;
import com.wc.bean.MList;
import com.wc.bean.Menu;
import com.wc.bean.ResponseMsg;
import com.wc.bean.User;
import com.wc.service.MailService;
import com.wc.service.WcMenuService;
import com.wc.service.WcUserService;
import com.wc.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody  //将方法的返回值，以特定的格式(json格式的数据)写入到response的body区域，进而将数据返回给客户端。
//@RestController
@CrossOrigin(origins = "*")
public class WcUserController {

    @RequestMapping("/")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("men","hello index");
        mv.setViewName("/index");
        return mv;
    }

    @Autowired
    WcUserService userService;
    MailService mailService;

    /**
     * http://localhost:8089/test/register?id=13&userName=zhangsan&password=123&nickName=z
     * @param user
     * @return
     */

    @RequestMapping("/register")
    public  int register(@RequestBody User user){
        return  userService.register(user);
    }

    @RequestMapping("/loginHtml")
    public ModelAndView loginHtml(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("men","hello index");
        mv.setViewName("/login");
        return mv;
    }

    @RequestMapping("/login")  //id  password
    public  List<User> login(@RequestBody Map<String,Object> map){
        List<User> userList = userService.login(map);
        return userList;
//        // 打印了包含两个参数的日志信息
//        Logger log = null;
//        log.info("userMap: {}", map);
//        // 调用返回List<User>的方法
//        List<User> userList = userService.login(map);
//        Map<String, Object> nmap = new HashMap<>();
//        try {
//            if (userList != null && !userList.isEmpty()) {
//                // 如果列表不为空，表示认证成功
//                User u = userList.get(0);  // 假设取第一个用户
//                Map<String, Object> payLoad = new HashMap<>();
//                payLoad.put("id", u.getId());
//                payLoad.put("userName", u.getUserName());
//                String token = JWTUtils.getToken(payLoad);
//                map.put("state", true);
//                map.put("message", "认证成功");
//                map.put("token", token);
//            } else {
//                // 如果列表为空，表示用户名或密码错误
//                map.put("state", false);
//                map.put("message", "用户名或者密码错误");
//            }
//        } catch (Exception e) {
//            map.put("state", false);
//            map.put("msg", e.getMessage());
//        }
//        return (ResponseMsg) nmap;
    }

    @RequestMapping("/registerHtml")
    public ModelAndView registerHtml(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("men","hello index");
        mv.setViewName("/register");
        return mv;
    }

    @RequestMapping("/vistorHtml")
    public ModelAndView vistorHtml(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("men","hello index");
        mv.setViewName("/register");
        return mv;
    }


    @RequestMapping("/fkHtml")
    public ModelAndView fkHtml(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("men","hello index");
        mv.setViewName("/fk");
        return mv;
    }
    @RequestMapping("/email")
    public int email(@RequestBody String email){
        mailService.sendMail(email,"测试主题","测试内容：b210601测试");
        return 1;
    }

    @RequestMapping("/menu")  //uid
    public  List<Menu> selectMenu(@RequestBody Map<String,Object> map){
        return  userService.selectRole(userService.selectRname(map));
    }


    @Autowired
    WcMenuService menuService;

    @RequestMapping("/menu/selectlist")  //mid
    public List<MList> selectList(@RequestBody Map<String,Object> map){
        return  menuService.selectList(map);
    }

    @RequestMapping("/menu/insertlist")  //#{mid},#{listname},#{thing},#{amount}
    public int insertList(@RequestBody Map<String,Object> map){
        return  menuService.insertList(map);
    }

    @RequestMapping("/menu/admin/update")
    public int updateListstate(@RequestBody Map<String,Object> map){    //lid
        return  menuService.updateListstate(map);
    }
    @RequestMapping("/menu/delete")
    public int deleteList(@RequestBody Map<String,Object> map){    //lid

        return  menuService.deleteList(map);
    }

}